package com.bana.fvs.protocol;

import com.bana.fvs.model.VisibilityScale;
import com.bana.sys.utils.Convert;

import java.io.*;
import java.util.List;

/**
 * 设置控制器能见度等级参数
 */
public class SetVisibilityConfig {

    public int getId()
    {
        return 0x07;
    }
    
    public byte[] toBuffer(int kzqbsm,List<VisibilityScale> visibilityScales) throws IOException {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeByte(0x81);
        dos.writeByte(kzqbsm);
        dos.writeByte(getId());

        //固定十个等级，每个等级12字节
        for(int i=0;i<visibilityScales.size();++i)
        {
            dos.writeByte(visibilityScales.get(i).getNjddj());
            dos.writeByte(visibilityScales.get(i).getNjdsx()%128);
            dos.writeByte(visibilityScales.get(i).getNjdsx()/128);
            dos.writeByte(visibilityScales.get(i).getWdkz());
            dos.writeByte(visibilityScales.get(i).getWdld());
            dos.writeByte(visibilityScales.get(i).getLdsc()%128);
            dos.writeByte(visibilityScales.get(i).getLdsc()/128);
            dos.writeByte(visibilityScales.get(i).getMdsc()%128);
            dos.writeByte(visibilityScales.get(i).getMdsc()/128);
            dos.writeByte(visibilityScales.get(i).getHdys());
            dos.writeByte(visibilityScales.get(i).getMqys());
            dos.writeByte(visibilityScales.get(i).getHsjsdzsl());
        }
        dos.writeByte(0x00);
        dos.writeByte(0xFF);
        byte[] buffer= baos.toByteArray();
        byte b= Convert.xor(buffer,3,120);
        buffer[123]=b;
        dos.close();
        baos.close();
        return buffer;
    }

    public boolean toResult(byte[] buffer)
    {
        return buffer.length==4;
    }
}
